home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Doors_System / ExtractDIZ / Main.C < prev    next >
C/C++ Source or Header  |  1996-10-31  |  13KB  |  433 lines

  1. /*
  2.  
  3.   todo
  4.   ----
  5.  
  6.   remove repetitive code,  structure code more..
  7.  
  8.   set F-<flag> to NULL or FALSE between loaded flags!! if you had the following config
  9.   you'd get problems
  10.  
  11.     Get_RenameDIZ_1=TRUE
  12.                            > as _2 is missing it'd be TRUE where it should default to FALSE
  13.     Get_RenameDIZ_3=TRUE
  14.  
  15.   ExtractDIZ/AddDIZ
  16.   ====================
  17.  
  18.   This door has two modes, extract diz and add diz, I've written one door for the
  19.   two functions as alot of the code is the same.
  20.  
  21.   Program to extract the file_id.diz from a file, store the fileid in the node's work
  22.   dir as <filename>.diz
  23.  
  24.   Options
  25.   =======
  26.  
  27.     N_ND->ActiveDoor->SystemOptions
  28.     -------------------------------
  29.  
  30.     UPDATE|EXTRACT
  31.  
  32.     Door Options (Command Line)
  33.     ---------------------------
  34.  
  35. 2   <filename>
  36.  
  37. 3   <workdir>
  38.  
  39. 4   <playpen dir>
  40.  
  41. 5   <filename without .extension>
  42.  
  43. 6   <.extension without filename and without the .>
  44.  
  45.     Note: Hey, I really couldn't think of any other settings that might be usefull..
  46.     you can even write a damn AmigaDOS script to extract file_id's with all those
  47.     parameters! hehehe
  48.  
  49.  
  50. */
  51.  
  52.  
  53. #include <exec/types.h>
  54. #include <exec/memory.h>
  55. #include <dos/dos.h>
  56. #include <clib/exec_protos.h>
  57. #include <clib/dos_protos.h>
  58. #include <clib/alib_protos.h>
  59.  
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <stdio.h>
  63. #include <ctype.h>
  64. #include <time.h>
  65.  
  66.  
  67. #ifdef __SASC
  68. int CXBRK(void) { return(0); }
  69. int _CXBRK(void) { return(0); }
  70. void chkabort(void) {}
  71. #endif
  72.  
  73. #include <HBBS/ANSI_Codes.h>
  74. #include <HBBS/Defines.h>
  75. #include <HBBS/types.h>
  76. #include <HBBS/structures.h>
  77. #include <HBBS/hbbscommon_protos.h>
  78. #include <HBBS/hbbscommon_pragmas.h>
  79. #include <HBBS/Hbbsnode_protos.h>
  80. #include <HBBS/Hbbsnode_pragmas.h>
  81. #include <HBBS/release.h>
  82. char *versionstr="$VER: ExtractDIZ "RELEASE_STR;
  83.  
  84. struct Library *HBBSCommonBase=NULL;
  85. struct Library *HBBSNodeBase=NULL;
  86.  
  87. struct BBSGlobalData *BBSGlobal=NULL;
  88. struct NodeData *N_ND=NULL;
  89. int N_NodeNum=-1;
  90. char outstr[1024]; // temp string for displaying text..
  91.  
  92. char     *F_Ext=NULL;
  93. char     *F_Type=NULL;
  94. char     *F_AddDIZ=NULL;
  95. char     *F_GetDIZ=NULL;
  96. V_BIGNUM  F_MaxDIZLines=0;
  97. V_BOOL    F_Add_CDToWork=FALSE;
  98. V_BOOL    F_Get_CDToWork=FALSE;
  99. V_BOOL    F_Add_CDToPlayPen=FALSE;
  100. V_BOOL    F_Get_CDToPlayPen=FALSE;
  101. V_BOOL    F_Get_RenameDIZ=FALSE;
  102. V_BOOL    F_Add_RenameDIZ=FALSE;
  103.  
  104.  
  105. LONG __stack=16384;   // needs big stack for running external commands!
  106.  
  107. static VOID cleanup(ULONG num)
  108. {
  109.   if (HBBSNodeBase)
  110.   {
  111.     HBBS_CleanUpDoor();
  112.     CloseLibrary (HBBSNodeBase);
  113.   }
  114.  
  115.   if (HBBSCommonBase)
  116.   {
  117.     HBBS_CleanUpCommon();
  118.     CloseLibrary (HBBSCommonBase);
  119.   }
  120.  
  121.   if (num) printf("Door Error = %d\n",num);
  122.  
  123.   exit(0);
  124. }
  125.  
  126. static VOID init(char *name)
  127. {
  128.   if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  129.   {
  130.     cleanup(1);
  131.   }
  132.  
  133.   if (!(HBBS_InitCommon()))
  134.   {
  135.     cleanup(2);
  136.   }
  137.  
  138.   if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  139.   {
  140.     cleanup(3);
  141.   }
  142.  
  143.   if (!(HBBS_InitDoor(N_NodeNum,name)))
  144.   {
  145.     cleanup(4);
  146.   }
  147.   SetProgramName(name);
  148. }
  149.  
  150. #define MODE_UPDATE 1
  151. #define MODE_EXTRACT 2
  152.  
  153.  
  154. /*
  155. ; {A} is replaced with the full path and filename of the uploaded file
  156. ; {F} is replaced with the filename of the uploaded file
  157. ; {N} is replaced with the filename (but not the extension) of the file
  158. ; {E} is replaced with the extension of the file
  159. ; {W} is replaced with the full path of the WORK directory
  160. ; {P} is replaced with the playpen direcotry
  161. ; {D} is replaced with the full path and filename of the DIZ file to be added/extracted
  162. */
  163.  
  164. void ExtractDIZ(int argc,char *argv[], char *TypeOfFile )
  165. {
  166.   char runcmd[2048]; //*W* define me
  167.   char tmpstr[2048];
  168.  
  169.  
  170.   sprintf(tmpstr,ANSI_RESET ANSI_FG_GREEN "Extracting File ID for "ANSI_ITALIC ANSI_FG_BLUE "%s" ANSI_RESET ANSI_FG_GREEN " \"%s\" .. ",TypeOfFile,argv[2]);
  171.   DOOR_WriteText(tmpstr);
  172.  
  173.   strcpy(tmpstr,argv[3]); // workdir
  174.   strcat(tmpstr,"file_id.diz");
  175.   DeleteFile(tmpstr); //remove old file
  176.  
  177.   strcpy(runcmd,F_GetDIZ);
  178.  
  179.   strcpy(tmpstr,argv[4]);
  180.   strcat(tmpstr,argv[2]);
  181.   replace(runcmd,runcmd,"{A}",tmpstr); //full path + filename
  182.  
  183.   replace(runcmd,runcmd,"{F}",argv[2]); //filename
  184.  
  185.   replace(runcmd,runcmd,"{N}",argv[5]); //filename without .extension
  186.  
  187.   replace(runcmd,runcmd,"{E}",argv[6]); //.extenstion
  188.  
  189.   replace(runcmd,runcmd,"{W}",argv[3]); //workdir
  190.  
  191.   replace(runcmd,runcmd,"{P}",argv[4]); //playpen dir
  192.  
  193.   strcpy(tmpstr,argv[3]);
  194.   strcat(tmpstr,argv[2]);
  195.   strcat(tmpstr,".ADD");
  196.   DeleteFile(tmpstr); //remove old file
  197.  
  198.   strcpy(tmpstr,argv[3]);
  199.   strcat(tmpstr,argv[2]);
  200.   strcat(tmpstr,".MISC");
  201.   DeleteFile(tmpstr); //remove old file
  202.  
  203.   strcpy(tmpstr,argv[3]);
  204.   strcat(tmpstr,argv[2]);
  205.   strcat(tmpstr,".DIZ");
  206.   DeleteFile(tmpstr); //remove old file
  207.   replace(runcmd,runcmd,"{D}",tmpstr); //workdir + filename + .DIZ
  208.  
  209.   HBBS_RunDOSCMD(runcmd,FALSE);
  210.  
  211.   if (F_Get_RenameDIZ)
  212.   {
  213.     strcpy(runcmd,argv[3]);
  214.     strcat(runcmd,"FILE_ID.DIZ");
  215.     Rename(runcmd,tmpstr);         // *** tmpstr must contain {D} still... ^^^
  216.   }
  217.  
  218.   DOOR_WriteText("Done\r\n");
  219. }
  220.  
  221. void UpdateDIZ(int argc,char *argv[])
  222. {
  223.   char runcmd[2048]; //*W* define me
  224.   char tmpstr[1048];
  225.   char tmpstr2[1048];
  226.  
  227.   DOOR_WriteText(ANSI_RESET "Updating File ID..");
  228.   strcpy(tmpstr,argv[3]); // workdir
  229.   strcat(tmpstr,"file_id.diz");
  230.   DeleteFile(tmpstr); //remove old file
  231.  
  232.   strcpy(runcmd,F_AddDIZ);
  233.  
  234.   strcpy(tmpstr,argv[4]);
  235.   strcat(tmpstr,argv[2]);
  236.   replace(runcmd,runcmd,"{A}",tmpstr); //full path + filename
  237.  
  238.   replace(runcmd,runcmd,"{F}",argv[2]); //filename
  239.  
  240.   replace(runcmd,runcmd,"{N}",argv[5]); //filename without .extension
  241.  
  242.   replace(runcmd,runcmd,"{E}",argv[6]); //.extenstion
  243.  
  244.   replace(runcmd,runcmd,"{W}",argv[3]); //workdir
  245.  
  246.   replace(runcmd,runcmd,"{P}",argv[4]); //playpen dir
  247.  
  248.   strcpy(tmpstr,argv[3]);
  249.   strcat(tmpstr,argv[2]);
  250.   strcat(tmpstr,".ADD");
  251.   replace(runcmd,runcmd,"{D}",tmpstr); //workdir + filename + .ADD
  252.  
  253.   if (F_Add_RenameDIZ) // rename <file>.ADD to file_id.diz before adding ?
  254.   {
  255.     strcpy(tmpstr2,argv[3]);
  256.     strcat(tmpstr2,"FILE_ID.DIZ");
  257.     Rename(tmpstr,tmpstr2);         // *** tmpstr must contain {D} still... ^^^
  258.   }
  259.  
  260.   HBBS_RunDOSCMD(runcmd,FALSE);
  261.  
  262.   DOOR_WriteText("Done\r\n");
  263. }
  264.  
  265.  
  266. void DoorMain(int argc,char *argv[])
  267. {
  268.   BPTR FL,NFL;
  269.   struct CfgFileData *CfgFile=NULL;
  270.   int ftype=1;
  271.   char optionname[30];
  272.  
  273.   short Mode=0;
  274.  
  275.   BOOL Done=FALSE,allfound;
  276.  
  277. //  DOOR_WriteText("\r\n-=[  Hydra ExtractDIZ V1.0  ]=- ======== -=[  (C) 1995 Deluxe Software Ltd  ]=-\r\n\r\n");
  278.  
  279.   if (stricmp("EXTRACT",N_ND->ActiveDoor->SystemOptions)==0) Mode=MODE_EXTRACT;
  280.   if (stricmp("UPDATE",N_ND->ActiveDoor->SystemOptions)==0) Mode=MODE_UPDATE;
  281.  
  282.  
  283.   if (!Mode)
  284.   {
  285.     DOOR_WriteText(ANSI_RESET ANSI_FG_RED "Invalid/No Mode specified!\r\n");
  286.   }
  287.   else
  288.   {
  289.     if (argc==6 || argc==7)
  290.     {
  291.       if (CfgFile=HBBS_LoadConfig("HBBS:System/FileID",LCFG_NONE))
  292.       {
  293.         do
  294.         {
  295.           sprintf(optionname,"FileExtension_%d",ftype);
  296.           if (HBBS_GetSetting(CfgFile,(void *)&F_Ext,VTYPE_STRING,optionname,OPT_SINGLE))
  297.           {
  298.             allfound=FALSE;
  299.             sprintf(optionname,"FileType_%d",ftype);
  300.             if (HBBS_GetSetting(CfgFile,(void *)&F_Type,VTYPE_STRING,optionname,OPT_SINGLE))
  301.             {
  302.               sprintf(optionname,"AddDIZ_%d",ftype);
  303.               if (HBBS_GetSetting(CfgFile,(void *)&F_AddDIZ,VTYPE_STRING,optionname,OPT_SINGLE))
  304.               {
  305.                 sprintf(optionname,"GetDIZ_%d",ftype);
  306.                 if (HBBS_GetSetting(CfgFile,(void *)&F_GetDIZ,VTYPE_STRING,optionname,OPT_SINGLE))
  307.                 {
  308.                   sprintf(optionname,"MaxDIZLines_%d",ftype);
  309.                   HBBS_GetSetting(CfgFile,(void *)&F_MaxDIZLines,VTYPE_BIGNUM,optionname,OPT_SINGLE);
  310.                   sprintf(optionname,"Add_CDToWork_%d",ftype);
  311.                   HBBS_GetSetting(CfgFile,(void *)&F_Add_CDToWork,VTYPE_BOOL,optionname,OPT_SINGLE);
  312.                   sprintf(optionname,"Get_CDToWork_%d",ftype);
  313.                   HBBS_GetSetting(CfgFile,(void *)&F_Get_CDToWork,VTYPE_BOOL,optionname,OPT_SINGLE);
  314.                   sprintf(optionname,"Add_CDToPlayPen_%d",ftype);
  315.                   HBBS_GetSetting(CfgFile,(void *)&F_Add_CDToPlayPen,VTYPE_BOOL,optionname,OPT_SINGLE);
  316.                   sprintf(optionname,"Get_CDToPlayPen_%d",ftype);
  317.                   HBBS_GetSetting(CfgFile,(void *)&F_Get_CDToPlayPen,VTYPE_BOOL,optionname,OPT_SINGLE);
  318.                   sprintf(optionname,"Get_RenameDIZ_%d",ftype);
  319.                   HBBS_GetSetting(CfgFile,(void *)&F_Get_RenameDIZ,VTYPE_BOOL,optionname,OPT_SINGLE);
  320.                   sprintf(optionname,"Add_RenameDIZ_%d",ftype);
  321.                   HBBS_GetSetting(CfgFile,(void *)&F_Add_RenameDIZ,VTYPE_BOOL,optionname,OPT_SINGLE);
  322.  
  323.                   // Required and Optional params are now loaded..
  324.                   allfound=TRUE;
  325.  
  326.                   if (stricmp(F_Ext,argv[6])==0)
  327.                   {
  328.                     if (Mode==MODE_EXTRACT)
  329.                     {
  330.                       if (F_Get_CDToPlayPen)
  331.                       {
  332.                         if (FL=Lock(N_ND->NodeSettings.NodePlayPen,ACCESS_READ))
  333.                         {
  334.                           NFL=CurrentDir(FL);
  335.                           UnLock(NFL);
  336.                           ExtractDIZ(argc,argv,F_Type);
  337.                         } else DOOR_WriteText("Lock(<Playpen>) Failed!\r\n");
  338.                       }
  339.                       else
  340.                       {
  341.                         if (F_Get_CDToWork)
  342.                         {
  343.  
  344.                           sprintf(outstr,"%sWork/",N_ND->NodeLocation);
  345.                           if (FL=Lock(outstr,ACCESS_READ))
  346.                           {
  347.                             NFL=CurrentDir(FL);
  348.                             UnLock(NFL);
  349.                             ExtractDIZ(argc,argv,F_Type);
  350.                           } else DOOR_WriteText("Lock(<playpen>) Failed!\r\n");
  351.                         }
  352.                         else ExtractDIZ(argc,argv,F_Type);
  353.                       }
  354.                     }
  355.                     else
  356.                     {
  357.                       if (Mode==MODE_UPDATE)
  358.                       {
  359.                         if (F_Get_CDToPlayPen)
  360.                         {
  361.                           if (FL=Lock(N_ND->NodeSettings.NodePlayPen,ACCESS_READ))
  362.                           {
  363.                             NFL=CurrentDir(FL);
  364.                             UnLock(NFL);
  365.                             UpdateDIZ(argc,argv);
  366.                           } else DOOR_WriteText("Lock(<Playpen>) Failed!\r\n");
  367.                         }
  368.                         else
  369.                         {
  370.                           if (F_Get_CDToWork)
  371.                           {
  372.  
  373.                             sprintf(outstr,"%sWork/",N_ND->NodeLocation);
  374.                             if (FL=Lock(outstr,ACCESS_READ))
  375.                             {
  376.                               NFL=CurrentDir(FL);
  377.                               UnLock(NFL);
  378.                               UpdateDIZ(argc,argv);
  379.                             } else DOOR_WriteText("Lock(<playpen>) Failed!\r\n");
  380.                           }
  381.                           else UpdateDIZ(argc,argv);
  382.                         }
  383.                       }
  384.  
  385.                     }
  386.  
  387.                   }
  388.                 }
  389.               }
  390.             }
  391.             if (!allfound)
  392.             {
  393.               sprintf(outstr,ANSI_RESET ANSI_FG_RED "Error In Configuration when loading options for\r\n"
  394.                              "%s files in the FileID Config File..\r\n",F_Ext);
  395.               DOOR_WriteText(outstr);
  396.             }
  397.           } else Done=TRUE;
  398.  
  399.           FreeStr(F_Ext); F_Ext=NULL;
  400.           FreeStr(F_Type); F_Type=NULL;
  401.           FreeStr(F_AddDIZ); F_AddDIZ=NULL;
  402.           FreeStr(F_GetDIZ); F_GetDIZ=NULL;
  403.           ftype++;
  404.         } while (!Done);
  405.  
  406.  
  407.         HBBS_FlushConfig(CfgFile);
  408.       }
  409.       else DOOR_WriteText(ANSI_RESET ANSI_FG_RED "Error Loading Configuration File!\r\n");
  410.     }
  411.     else DOOR_WriteText(ANSI_RESET ANSI_FG_RED "Invalid Door Options!\r\n");
  412.   }
  413. }
  414.  
  415. int main(int argc,char *argv[])
  416. {
  417.   if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  418.   {
  419.     printf("Invalid/No Paramaters for door!\n");
  420.     exit (20);
  421.   }
  422.   init("ExtractDIZ");
  423.  
  424.   if (BBSGlobal=HBBS_GimmeBBS())
  425.   {
  426.     if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
  427.     {
  428.       DoorMain(argc,argv);
  429.     }
  430.   }
  431.   cleanup(0);
  432. }
  433.